package com.steam.job;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.steam.entity.ItemsTest;
import com.steam.entity.ItemsTestExample;
import com.steam.entity.Phone;
import com.steam.mapper.ItemMapper;
import com.steam.mapper.ItemsTestMapper;
import com.steam.service.JedisClient;
import com.steam.util.GsonBuilderUtil;

@Component
public class GuolianSpringJob {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private JedisClient jedisClient;
	// private static SqlSessionFactory sqlSessionFactory;
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemsTestMapper itemsTestMapper;
	Gson gson = GsonBuilderUtil.create();

	/*
	 * static { String resource = "mybatis/GuolianSqlMap.xml"; InputStream
	 * inputStream; try { inputStream = Resources.getResourceAsStream(resource);
	 * sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); }
	 * catch (IOException e1) { e1.printStackTrace(); } }
	 */
	/**
	 * 每天的凌晨7点到23点 每隔5秒取出数据
	 */
	@Scheduled(cron = "0/1 * 7-23  * * ? ")
	// @Scheduled(fixedDelay=200)
	public void jining() {
		// 数据库会话实例
		// SqlSession sqlSession = null;
		// 国联过过来Example example = new 国联过过来Example();
		ItemsTestExample example = new ItemsTestExample();
		try {
			// 创建数据库会话实例sqlSession
			// sqlSession = sqlSessionFactory.openSession();
			Gson gson = GsonBuilderUtil.create();
			// 使用mybatis推荐的Mapper接口的方式,而不是使用ibatis的方式
			String jishu = jedisClient.get("lguolian");
			if (StringUtils.isNotBlank(jishu)) {
				example.createCriteria().andItemIdGreaterThan(Long.parseLong(jishu));
			} else {
				Integer selectMaxId = itemsTestMapper.selectMaxId();
				if (selectMaxId != null) {
					example.createCriteria().andItemIdGreaterThanOrEqualTo((long) selectMaxId);
				}
			}
			// 国联过过来Mapper mapper = sqlSession.getMapper(国联过过来Mapper.class);
			List<ItemsTest> selectByExamples = itemsTestMapper.selectByExampleWithBLOBs(example);
			// if (selectByExamples.size() == 0) {// 没有取出数据
			// ItemsTest selectByPrimaryKey =
			// itemsTestMapper.selectByPrimaryKey(Long.parseLong(jishu));//
			// 根据目前的编号来查询
			// if (selectByPrimaryKey != null) {
			//
			// } else {
			//// 国联过过来 selectByPrimaryKey2 = mapper.selectByPrimaryKey(1);
			//// if (System.currentTimeMillis() -
			// selectByPrimaryKey2.get发送时间1().getTime() > 600) {
			////
			//// } else {
			// jedisClient.set("guolian", "0");
			//// }
			// }
			// } else
			if (selectByExamples != null && selectByExamples.size() > 0) {// 如果取出了数据我们遍历数据进行处理
																			// 1000
																			// 60*20=1200
				for (ItemsTest tblinfo : selectByExamples) {
					/*
					 * if(tblinfo.getStartcity().equals("临沂")){ continue; }
					 */
					List<Phone> phone_arr = new ArrayList<>();
					String o_phones = tblinfo.getPhone();
					String[] phoneList = o_phones.split(" ");
					if (phoneList != null && phoneList.length > 0) {
						for (int i = 0; i < phoneList.length; i++) {
							Phone phone = new Phone();
							phone.setPhone(phoneList[i]);
							phone_arr.add(phone);
						}
					}
					com.steam.entity.Item items = new com.steam.entity.Item();
					items.setCreateTime(tblinfo.getTime());// 创建信息的时间
					items.setUserPhones(tblinfo.getPhone());// 电话信息
					items.setContent(tblinfo.getContent());// 信息的内容
					items.setCity(tblinfo.getStartcity());// 出发地 市
					items.setFormin(tblinfo.getSource());// 数据源的来源
					items.setTypeId(tblinfo.getTypeId());
					// 推送给德州网平台bean
					Item item = new Item();
					item.setContent(tblinfo.getContent());
					// item.setPhones(sb.toString().trim());
					item.setTime(tblinfo.getTime());
					User user = new User();
					user.setPhoneList(phone_arr);
					item.setUser(user);
					item.setlTime(tblinfo.getTime().getTime());
					item.setTypeId(tblinfo.getTypeId());
					try {
						//itemMapper.insertSelective(items);
					} catch (Exception e) {
						e.printStackTrace();
						jedisClient.set("lguolian", tblinfo.getItemId().toString());
					}
					if(!items.getCity().equals("德州")){
						rabbitTemplate.convertAndSend("*."+items.getCity(), gson.toJson(item));
					}
					//rabbitTemplate.convertAndSend("*." + items.getCity(), gson.toJson(item));
					jedisClient.set("lguolian", tblinfo.getItemId().toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}